/* 前台飞机票出发地和目的地交换 hanxl 2021.03.02 单程*/
function exchange(){
	var homecity_name=$("#homecity_name1").val();
	var cityid1=$("#cityid1").val();

	var getcity_name=$("#getcity_name1").val();
	var getcityid1=$("#getcityid1").val();
	$("#homecity_name1").val(getcity_name);
	$("#cityid1").val(getcityid1);
	$("#getcity_name1").val(homecity_name);
		$("#getcityid1").val(cityid1);
	return true;
}
/* 前台飞机票出发地和目的地交换 hanxl 2021.03.02 往返*/
function exchanges(){
	var homecity_name=$("#homecity_name2").val();
	var cityid2=$("#cityid2").val();

	var getcity_name=$("#getcity_name2").val();
	var getcityid2=$("#getcityid2").val();
	$("#homecity_name2").val(getcity_name);
	$("#cityid2").val(getcityid2);
	$("#getcity_name2").val(homecity_name);
		$("#getcityid2").val(cityid2);
	return true;
}
//前台机票菜单首页 预留验证 hanxl 2021.03.17
function reserveTrain(iPlanID,iUserID){
	if(iUserID==null){
		
		 var txt=  "你还未登录，请您先登录！";
 	   window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.warning);
		/*	$.messager.alert('警告','你还未登录，请您先登录！','warning');*/
    return false;
	}else{
		window.location.href="enterreservetrain?iPlanID=" + iPlanID;	
	}
}
/*机票动态加载订单人表格 2021.01.15 hyanxl */
function selectTrainOrderNum(){	
	
var number=$("#planNum" ).val();
var price=$("#planPrice").val();
var totol=parseInt(price) * parseInt(number) ;
$("#reservePrice").val(parseInt(totol));
	$("#showInfo").html("");
		
			var num=0;
			
			if(!totol=='0'){
			var div=$("#showInfo");
			var div1=$("<div id='planInfo' class='reserveTicket'></div>")
			var h3=$("<h3>出行人信息</h3>");
			div1.append(h3);
			
			var no=$("#planNum" ).val();
				if(no!=0){
				var div2=$("<div></div>")
				var planName=$("#planStandard").val();
				var h5=$("<h5>"+planName+"</h5>");
				var table=$("<table class='info-table' id='orderInfo'></table>");
					for(var j=0;j<no;j++){
					var tr=$("<tr id='tr"+j+"'></tr>")
					var th1=$("<th><label for='orderName"+num+"'>姓名</label></th>");
					var td1=$("<td> </td>");
					var input=$("<input type='text' class='scenicorderinput' id='orderName"+num+"' name='OrderName' maxlength='16' required/>");
					td1.append(input);
					var th2=$("<th><label for='orderTel"+num+"'>手机号</label></th>");
					var td2=$("<td></td>");
					var input2=$("<input type='text' class='scenicorderinput' id='orderTel"+num+"' name='OrderTel' maxlength='16' required />");
					td2.append(input2);
					var th3=$("<th><label for='orderIdentity"+num+"'>身份证号</label></th>");
					var td3=$("<td></td>");
					var input3=$("<input type='text' class='scenicorderinput' id='orderIdentity"+num+"' name='Identity' maxlength='18' required/>");
					td3.append(input3);
					tr.append(th1);
					tr.append(td1);
					tr.append(th2);
					tr.append(td2);
					tr.append(th3);
					tr.append(td3);
					table.append(tr);
					num++;
					}
				div2.append(h5);
		div2.append(table);
				}
	div1.append(div2);
			

			div.append(div1);
			}
}
/* 前台飞机票订单提交 hanxl 2021.03.17 */
function saveReserveTrain(){
  
	//如果价格为零 提示需要选择票种
	if($("#reservePrice").val()=="--" ||parseInt($("#reservePrice").val())==0){
			$.messager.alert('警告','请选择火车票数量','warning');
		$("#planNum").focus();				
		return false;
	}
	
	//获取一共有多少机票
	
	var num=parseInt($("#planNum" ).val());
	
/*for( var j=0; j<num ; j++ ){
	if($("#orderName"+j).val() == "" || $("#orderName"+j).val() == null){
		$.messager.alert('错误','姓名为空，请输入','error');
		$("#orderName"+j).focus();
						
		return false;
	}
	if($("#orderTel"+j).val() == "" || $("#orderTel"+j).val() == null){
		
		$.messager.alert('错误','联系方式为空，请输入','error');
		$("#orderTel"+j).focus();
						
		return false;
	}else{
		if(!(/^1[3456789]\d{9}$/.test($("#orderTel"+j).val()))){ 
	
$.messager.alert('错误','订单人手机号格式错误！','error');
 						$("#orderTel"+j).focus();
		return false;
    }
	}
	if($("#orderIdentity"+j).val() == "" || $("#orderIdentity"+j).val() == null){
				$.messager.alert('错误','身份证号为空，请输入','error');
 				$("#orderIdentity"+j).focus();
						
		return false;
	}else{
		 if(!(/(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$)/.test($("#orderIdentity"+j).val()))){ 
	
 				$.messager.alert('错误','订单人身份证号格式错误！','error');
 							$("#orderIdentity"+j).focus();
						return false;	
    } 
	}
}*/
for( var j=0; j<num ; j++ ){
	if($("#orderName"+j).val() == "" || $("#orderName"+j).val() == null){
		var txt = "姓名为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#orderName"+j).focus();
						}});
		return false;
	}
	if($("#orderTel"+j).val() == "" || $("#orderTel"+j).val() == null){
		var txt = "联系方式为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#orderTel"+j).focus();
						}});
		return false;
	}else{
		if(!(/^1[3456789]\d{9}$/.test($("#orderTel"+j).val()))){ 
	 var txt=  "订单人手机号格式错误！";
 				window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
						$("#orderTel"+j).focus();
						}});
	
		return false;
    }
	}
	if($("#orderIdentity"+j).val() == "" || $("#orderIdentity"+j).val() == null){
		var txt = "身份证号为空，请输入";
		window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
		$("#orderIdentity"+j).focus();
						}});
		return false;
	}else{
		 if(!(/(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$)/.test($("#orderIdentity"+j).val()))){ 
	 var txt=  "订单人身份证号格式错误！";
 				window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.error,{onOk:function(){
							$("#orderIdentity"+j).focus();
						}});
						return false;	
    } 
	}
}
var arrOrder=[];
		
		 var planID=$("#planID" ).val();
		 var number=$("#planNum" ).val();
		var length=parseInt(number);
		for(i=0;i<length;i++){
		var planeOrder= {};
		planeOrder['orderID']=planID;
		planeOrder['orderName']=$("#orderName" + i).val();
		planeOrder['orderTel']=$("#orderTel" + i).val();
		planeOrder['identity']=$("#orderIdentity" + i).val();
		arrOrder.push(planeOrder);
		}
	$.ajax({
		
		
	
		url:"/mmy/savereservetrain",
		type:"post",
		data:{
			"planID":planID,
			"number":number,
			"order":JSON.stringify(arrOrder),
			
			},
		dataType:"json",
		success:function(data){
			var lstID=data.lstID;
			
			$.messager.alert('成功','预留成功！','success');
				var type=$("#strUserType").val();
				if(type=='0'){
					location.href="/mmy/fronttotrainindex";
				}else{
					location.href="/mmy/topayordertrain?lstID="+lstID;
				}
			/*var txt = "预留成功！";
			window.wxc.xcConfirm(txt, window.wxc.xcConfirm.typeEnum.success,{onOk:function(){
				var type=$("#strUserType").val();
				if(type=='0'){
					location.href="/mmy/tofrontscenicindex";
				}else{
					location.href="/mmy/topayorderscenic?lstID="+lstID;
				}
			
			}});*/
		}
	})
}

